package com.meilai.project.controller.web.finance.reimburse;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.finance.reimburse.ReimburseApplyCheckDTO;
import com.meilai.project.dto.finance.reimburse.ReimburseApplyDTO;
import com.meilai.project.dto.finance.reimburse.ReimburseApplyListDTO;
import com.meilai.project.service.finance.reimburse.ReimburseApplyService;
import com.meilai.project.util.ThreadLocalUserUtil;
import com.meilai.project.vo.finance.reimburse.ReimburseApplyVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xhy
 * @since 2022-12-07
 */
@RestController
@RequestMapping("/api/web/reimburse_apply")
public class ReimburseApplyController {


    @Autowired
    private ReimburseApplyService service;

    @GetMapping("list")
    @ApiOperation(value = "列表分页")
    public ApiResult<Page<ReimburseApplyVO>> list(@ApiParam("form") ReimburseApplyListDTO req) {
        req.setCurr_user_id(null);
        if (!req.getManager_scenes()) {
            req.setCurr_user_id(ThreadLocalUserUtil.get().getId());
        }
        Page<ReimburseApplyVO> ReimburseApplyVOPage = service.selectPageList(req);
        return ApiResult.ok(ReimburseApplyVOPage);
    }

    @GetMapping("detail")
    @ApiOperation(value = "单据明细")
    public ApiResult<ReimburseApplyVO> detail(
            @ApiParam("id") @RequestParam(value = "id") Long id
    ) {
        return ApiResult.ok(service.getOneById(id));
    }

    @PostMapping("save")
    @ApiOperation("保存")
    public ApiResult<Boolean> save(@RequestBody ReimburseApplyDTO req) {
        try {
            if (service.save(req)) {
                return ApiResult.ok("新增成功");
            } else {
                return ApiResult.err("新增失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("begin")
    @ApiOperation("提交")
    public ApiResult<Boolean> begin(@RequestBody ReimburseApplyDTO req) {
        try {
            if (service.begin(req)) {
                return ApiResult.ok("提交成功");
            } else {
                return ApiResult.err("提交失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("pass")
    @ApiOperation("通过")
    public ApiResult<Boolean> pass(@RequestBody ReimburseApplyCheckDTO req) {
        try {
            if (service.pass(req)) {
                return ApiResult.ok("通过成功");
            } else {
                return ApiResult.err("通过失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("return_back")
    @ApiOperation("驳回")
    public ApiResult<Boolean> returnBack(@RequestBody ReimburseApplyCheckDTO req) {
        try {
            if (service.returnBack(req)) {
                return ApiResult.ok("驳回成功");
            } else {
                return ApiResult.err("驳回失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "逻辑删除")
    public ApiResult<Boolean> delete(@RequestParam("id") Long id) {
        try {
            if (service.delete(id)) {
                return ApiResult.ok("删除成功");
            } else {
                return ApiResult.err("删除失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @DeleteMapping("actual_delete")
    @ApiOperation(value = "物理删除")
    public ApiResult<Boolean> actualDelete(@RequestParam("id") Long id) {
        try {
            if (service.actualDelete(id)) {
                return ApiResult.ok("彻底删除成功");
            } else {
                return ApiResult.err("彻底删除失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }
}
